package interfaceGraphique;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionListener;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import metier.ElementListe;
import metier.ExceptionMetier;
import facade.FacadeAnneeEtude;
import facade.FacadeProgramme;

public class GestionSEEGUI extends JPanel implements ActionListener, ListSelectionListener {

    /**
     *
     */
    private final static String mSupprimerService = "Etes-vous sur de vouloir supprimer cette service ?\n";  //  @jve:decl-index=0:
    private FacadeProgramme facadeProgramme = new FacadeProgramme();  //  @jve:decl-index=0:
    private FacadeAnneeEtude facadeAnneeEtude = new FacadeAnneeEtude();
    private ElementListe elNouveauSee = null;  //  @jve:decl-index=0:
    private static final long serialVersionUID = 1L;
    private JLabel jLabel_semestre = null;
    private JComboBox jComboBox_semestre = null;
    private JLabel jLabel_SEE = null;
    private JComboBox jComboBox_SEE = null;
    private JLabel jLabel_matiere = null;
    private JLabel jLabel_type_enseignement = null;
    private JLabel jLabel_nomre_heures = null;
    private JComboBox jComboBox_matiere = null;
    private JComboBox jComboBox_type_enseignement = null;
    private JTextField jTextField_nombre_heures = null;
    private JLabel jLabel_nombre_heures_exemple = null;
    private JList jList_liste_services = null;
    private JTextArea jTextArea_description = null;
    private JButton jButton_service_plus = null;
    private JButton jButton_service_moins = null;
    private JButton jButton_service_modification = null;
    private JButton jButton_ajouter = null;
    private JButton jButton_modifier = null;
    private JButton jButton_supprimer = null;
    private JPanel jPanel_liste_services = null;
    private JPanel jPanel_description = null;
    private JScrollPane jScrollPane = null;
    private JScrollPane jScrollPane1 = null;
    private JPanel jPanel = null;
    private JPanel jPanel1 = null;
    private JPanel jPanelSEE = null;

    /**
     * This method initializes jComboBox_semestre
     *
     * @return javax.swing.JComboBox
     */
    private JComboBox getJComboBox_semestre() {
        if (jComboBox_semestre == null) {
            jComboBox_semestre = new JComboBox();
            jComboBox_semestre.setPreferredSize(new Dimension(200, 20));
            jComboBox_semestre.setMinimumSize(new Dimension(200, 20));
            jComboBox_semestre.addActionListener(this);
            jComboBox_semestre.addPopupMenuListener(new PopupMenuListener() {
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    refreshListeSemestre();
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
            });
            jComboBox_semestre.addActionListener(this);
            refreshListeSemestre();

        }
        return jComboBox_semestre;
    }

    /**
     * This method initializes jComboBox_SEE
     *
     * @return javax.swing.JComboBox
     */
    private JComboBox getJComboBox_SEE() {
        if (jComboBox_SEE == null) {
            jComboBox_SEE = new JComboBox();
            jComboBox_SEE.setPreferredSize(new Dimension(200, 20));
            jComboBox_SEE.setMinimumSize(new Dimension(200, 20));
            jComboBox_SEE.addActionListener(this);
            jComboBox_SEE.addPopupMenuListener(new PopupMenuListener() {
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    refreshListeSee();
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
            });
            jComboBox_SEE.addActionListener(this);
            refreshListeSee();

        }
        return jComboBox_SEE;
    }

    /**
     * This method initializes jComboBox_matiere
     *
     * @return javax.swing.JComboBox
     */
    private JComboBox getJComboBox_matiere() {
        if (jComboBox_matiere == null) {
            jComboBox_matiere = new JComboBox();
            jComboBox_matiere.setPreferredSize(new Dimension(200, 20));
            jComboBox_matiere.setMinimumSize(new Dimension(200, 20));
            jComboBox_matiere.addPopupMenuListener(new PopupMenuListener() {
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    refreshListeMatiere();
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
            });
            refreshListeMatiere();
        }
        return jComboBox_matiere;
    }

    /**
     * This method initializes jComboBox_type_enseignement
     *
     * @return javax.swing.JComboBox
     */
    private JComboBox getJComboBox_type_enseignement() {
        if (jComboBox_type_enseignement == null) {
            jComboBox_type_enseignement = new JComboBox();
            jComboBox_type_enseignement.setPreferredSize(new Dimension(200, 20));
            jComboBox_type_enseignement.setMinimumSize(new Dimension(200, 20));
            jComboBox_type_enseignement.addPopupMenuListener(new PopupMenuListener() {
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                    refreshListeTE();
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
                /* (non-Javadoc)
                 * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent)
                 */

                @Override
                public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                    // TODO Auto-generated method stub
                }
            });
            refreshListeTE();
        }
        return jComboBox_type_enseignement;
    }

    /**
     * This method initializes jTextField_nombre_heures
     *
     * @return javax.swing.JTextField
     */
    private JTextField getJTextField_nombre_heures() {
        if (jTextField_nombre_heures == null) {
            jTextField_nombre_heures = new JTextField();
            jTextField_nombre_heures.setPreferredSize(new Dimension(75, 20));
            jTextField_nombre_heures.setMinimumSize(new Dimension(75, 20));
        }
        return jTextField_nombre_heures;
    }

    /**
     * This method initializes jTextArea_description
     *
     * @return javax.swing.JTextArea
     */
    private JTextArea getJTextArea_description() {
        if (jTextArea_description == null) {
            jTextArea_description = new JTextArea();
            jTextArea_description.setWrapStyleWord(true);
            jTextArea_description.setLineWrap(true);
        }
        return jTextArea_description;
    }

    /**
     * This method initializes jList_liste_services
     *
     * @return javax.swing.JList
     */
    private JList getJList_liste_services() {
        if (jList_liste_services == null) {
            jList_liste_services = new JList();
            DefaultListModel modelo = new DefaultListModel();
            jList_liste_services.setModel(modelo);
            jList_liste_services.addListSelectionListener(this);




        }
        return jList_liste_services;
    }

    /**
     * This method initializes jButton_service_plus
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton_service_plus() {
        if (jButton_service_plus == null) {
            jButton_service_plus = new JButton();
            jButton_service_plus.setText("+");
            jButton_service_plus.setEnabled(false);
            jButton_service_plus.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    GestionServicesGUI pop = new GestionServicesGUI(null, facadeProgramme.getSee().getId(), "a", getJComboBox_SEE().getSelectedIndex());
                    pop.setLocationRelativeTo(jPanelSEE);
                    pop.setTitle("Ajout d'un service");
                    pop.setVisible(true);
                }
            });
        }
        return jButton_service_plus;
    }

    /**
     * This method initializes jButton_service_moins
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton_service_moins() {
        if (jButton_service_moins == null) {
            jButton_service_moins = new JButton();
            jButton_service_moins.setText("-");
            jButton_service_moins.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    int rep = JOptionPane.showConfirmDialog(null, mSupprimerService, "Confirmer la suppression", JOptionPane.YES_NO_OPTION);
                    if (rep == 0) {
                        facadeProgramme.creerService(null, null, null, null);
                        try {
                            facadeProgramme.getService().load(((ElementListe) getJList_liste_services().getSelectedValue()).getId());
                            facadeProgramme.supprimerService();
                            afficherStatut("Suppression effectu�");
                            refreshListeService();
                        } catch (ExceptionMetier e1) {
                            afficherStatut(e1.toString());
                            e1.printStackTrace();
                        }
                    }
                }
            });
        }
        return jButton_service_moins;
    }

    /**
     * This method initializes jButton_service_modification
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton_service_modification() {
        if (jButton_service_modification == null) {
            jButton_service_modification = new JButton();
            jButton_service_modification.setText("Modifier");
            jButton_service_modification.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    GestionServicesGUI pop = new GestionServicesGUI(null, facadeProgramme.getSee().getId(), "m", getJComboBox_SEE().getSelectedIndex());
                    pop.setLocationRelativeTo(jPanelSEE);
                    pop.setTitle("Modification d'un service");
                    pop.setVisible(true);
                }
            });
        }
        return jButton_service_modification;
    }

    /**
     * This method initializes jButton_ajouter
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton_ajouter() {
        if (jButton_ajouter == null) {
            jButton_ajouter = new JButton();
            jButton_ajouter.setText("Ajouter");
            jButton_ajouter.addActionListener(this);
        }
        return jButton_ajouter;
    }

    /**
     * This method initializes jButton_modifier
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton_modifier() {
        if (jButton_modifier == null) {
            jButton_modifier = new JButton();
            jButton_modifier.setText("Modifier");
            jButton_modifier.addActionListener(this);
        }
        return jButton_modifier;
    }

    /**
     * This method initializes jButton_supprimer
     *
     * @return javax.swing.JButton
     */
    private JButton getJButton_supprimer() {
        if (jButton_supprimer == null) {
            jButton_supprimer = new JButton();
            jButton_supprimer.setText("Supprimer");
            jButton_supprimer.addActionListener(this);
        }
        return jButton_supprimer;
    }

    /**
     * This method initializes jPanel_liste_services
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel_liste_services() {
        if (jPanel_liste_services == null) {
            GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
            gridBagConstraints16.gridx = 0;
            gridBagConstraints16.gridy = 1;
            GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
            gridBagConstraints5.fill = GridBagConstraints.BOTH;
            gridBagConstraints5.weighty = 1.0;
            gridBagConstraints5.weightx = 1.0;
            jPanel_liste_services = new JPanel();
            jPanel_liste_services.setLayout(new GridBagLayout());

            TitledBorder servicesBorder;
            servicesBorder = BorderFactory.createTitledBorder("Liste des services");
            jPanel_liste_services.setBorder(servicesBorder);
            jPanel_liste_services.add(getJScrollPane1(), gridBagConstraints5);
            jPanel_liste_services.add(getJPanel(), gridBagConstraints16);
        }
        return jPanel_liste_services;
    }

    /**
     * This method initializes jPanel_description
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel_description() {
        if (jPanel_description == null) {
            GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
            gridBagConstraints2.fill = GridBagConstraints.BOTH;
            gridBagConstraints2.weighty = 1.0;
            gridBagConstraints2.weightx = 1.0;
            jPanel_description = new JPanel();
            jPanel_description.setLayout(new GridBagLayout());

            TitledBorder descriptionBorder;
            descriptionBorder = BorderFactory.createTitledBorder("Description");
            jPanel_description.setBorder(descriptionBorder);
            jPanel_description.add(getJScrollPane(), gridBagConstraints2);

        }
        return jPanel_description;
    }

    /**
     * This is the default constructor
     */
    public GestionSEEGUI() {
        super();
        initialize();
    }

    /**
     * This method initializes this
     *
     * @return void
     */
    private void initialize() {
        GridBagConstraints gridBagConstraints20 = new GridBagConstraints();
        gridBagConstraints20.insets = new Insets(15, 3, 16, 4);
        gridBagConstraints20.gridy = 0;
        gridBagConstraints20.gridx = 0;
        jLabel_nombre_heures_exemple = new JLabel();
        jLabel_nombre_heures_exemple.setText("( Ex : 30 )");
        jLabel_nomre_heures = new JLabel();
        jLabel_nomre_heures.setText("Nombre d'heures :");
        jLabel_type_enseignement = new JLabel();
        jLabel_type_enseignement.setText("Type Enseignement : ");
        jLabel_matiere = new JLabel();
        jLabel_matiere.setText("Matiere :");
        jLabel_SEE = new JLabel();
        jLabel_SEE.setText("Choix du SEE :");
        jLabel_semestre = new JLabel();
        jLabel_semestre.setText("Choix du semestre :");
        this.setLayout(new GridBagLayout());
        this.setSize(new Dimension(667, 555));
        this.add(getJPanelSEE(), gridBagConstraints20);

    }

    /**
     * This method initializes jScrollPane
     *
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getJScrollPane() {
        if (jScrollPane == null) {
            jScrollPane = new JScrollPane();
            jScrollPane.setViewportView(getJTextArea_description());
            jScrollPane.setWheelScrollingEnabled(true);
            jScrollPane.setPreferredSize(new Dimension(400, 300));
            jScrollPane.setMinimumSize(new Dimension(400, 200));
        }
        return jScrollPane;
    }

    /**
     * This method initializes jScrollPane1
     *
     * @return javax.swing.JScrollPane
     */
    private JScrollPane getJScrollPane1() {
        if (jScrollPane1 == null) {
            jScrollPane1 = new JScrollPane();
            jScrollPane1.setWheelScrollingEnabled(true);
            jScrollPane1.setViewportView(getJList_liste_services());
            jScrollPane1.setPreferredSize(new Dimension(150, 200));
            jScrollPane1.setMinimumSize(new Dimension(300, 200));
        }
        return jScrollPane1;
    }

    /**
     * This method initializes jPanel
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel() {
        if (jPanel == null) {
            GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
            gridBagConstraints13.gridx = -1;
            gridBagConstraints13.insets = new Insets(5, 5, 5, 5);
            gridBagConstraints13.gridy = -1;
            GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
            gridBagConstraints12.anchor = GridBagConstraints.EAST;
            gridBagConstraints12.gridy = -1;
            gridBagConstraints12.insets = new Insets(0, 0, 0, 5);
            gridBagConstraints12.gridx = -1;
            GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
            gridBagConstraints9.gridx = -1;
            gridBagConstraints9.insets = new Insets(0, 0, 0, 5);
            gridBagConstraints9.gridy = -1;
            jPanel = new JPanel();
            jPanel.setLayout(new GridBagLayout());
            jPanel.add(getJButton_service_plus(), gridBagConstraints13);
            jPanel.add(getJButton_service_moins(), gridBagConstraints9);
            jPanel.add(getJButton_service_modification(), gridBagConstraints12);
        }
        return jPanel;
    }

    /**
     * This method initializes jPanel1
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJPanel1() {
        if (jPanel1 == null) {
            GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
            gridBagConstraints19.anchor = GridBagConstraints.CENTER;
            gridBagConstraints19.gridy = -1;
            gridBagConstraints19.insets = new Insets(0, 0, 10, 10);
            gridBagConstraints19.gridx = -1;
            GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
            gridBagConstraints17.anchor = GridBagConstraints.WEST;
            gridBagConstraints17.gridy = -1;
            gridBagConstraints17.insets = new Insets(0, 0, 10, 10);
            gridBagConstraints17.gridx = -1;
            GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
            gridBagConstraints18.anchor = GridBagConstraints.WEST;
            gridBagConstraints18.gridx = -1;
            gridBagConstraints18.gridy = -1;
            gridBagConstraints18.insets = new Insets(0, 0, 10, 10);
            jPanel1 = new JPanel();
            jPanel1.setLayout(new GridBagLayout());
            jPanel1.add(getJButton_ajouter(), gridBagConstraints18);
            jPanel1.add(getJButton_modifier(), gridBagConstraints17);
            jPanel1.add(getJButton_supprimer(), gridBagConstraints19);
        }
        return jPanel1;
    }

    /**
     * This method initializes jPanelSEE
     *
     * @return javax.swing.JPanel
     */
    private JPanel getJPanelSEE() {
        if (jPanelSEE == null) {
            GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
            gridBagConstraints61.anchor = GridBagConstraints.NORTH;
            gridBagConstraints61.insets = new Insets(10, 10, 10, 10);
            gridBagConstraints61.gridx = 0;
            gridBagConstraints61.gridy = 5;
            gridBagConstraints61.ipady = 0;
            gridBagConstraints61.weightx = 1.0;
            gridBagConstraints61.weighty = 1.0;
            gridBagConstraints61.fill = GridBagConstraints.VERTICAL;
            GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
            gridBagConstraints51.anchor = GridBagConstraints.WEST;
            gridBagConstraints51.gridwidth = 3;
            gridBagConstraints51.gridx = 1;
            gridBagConstraints51.gridy = 6;
            gridBagConstraints51.insets = new Insets(0, 10, 0, 0);
            GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
            gridBagConstraints41.anchor = GridBagConstraints.WEST;
            gridBagConstraints41.gridheight = 1;
            gridBagConstraints41.gridwidth = 1;
            gridBagConstraints41.gridx = 3;
            gridBagConstraints41.gridy = 4;
            gridBagConstraints41.insets = new Insets(0, 90, 0, 0);
            GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
            gridBagConstraints31.fill = GridBagConstraints.NONE;
            gridBagConstraints31.gridwidth = 3;
            gridBagConstraints31.gridx = 1;
            gridBagConstraints31.gridy = 5;
            gridBagConstraints31.anchor = GridBagConstraints.NORTH;
            gridBagConstraints31.insets = new Insets(10, 10, 10, 10);
            GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
            gridBagConstraints14.anchor = GridBagConstraints.WEST;
            gridBagConstraints14.insets = new Insets(5, 0, 5, 0);
            gridBagConstraints14.gridwidth = 2;
            gridBagConstraints14.gridx = 2;
            gridBagConstraints14.gridy = 4;
            gridBagConstraints14.weightx = 1.0;
            gridBagConstraints14.fill = GridBagConstraints.NONE;
            GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
            gridBagConstraints4.anchor = GridBagConstraints.WEST;
            gridBagConstraints4.insets = new Insets(0, 10, 5, 10);
            gridBagConstraints4.gridx = 0;
            gridBagConstraints4.gridy = 4;
            gridBagConstraints4.weightx = 1.0;
            gridBagConstraints4.fill = GridBagConstraints.NONE;
            GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
            gridBagConstraints3.anchor = GridBagConstraints.WEST;
            gridBagConstraints3.insets = new Insets(0, 10, 0, 0);
            gridBagConstraints3.gridx = 0;
            gridBagConstraints3.gridy = 3;
            gridBagConstraints3.fill = GridBagConstraints.NONE;
            GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
            gridBagConstraints1.anchor = GridBagConstraints.WEST;
            gridBagConstraints1.insets = new Insets(10, 10, 5, 10);
            gridBagConstraints1.gridx = 0;
            gridBagConstraints1.gridy = 2;
            gridBagConstraints1.weightx = 1.0;
            gridBagConstraints1.fill = GridBagConstraints.NONE;
            GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
            gridBagConstraints11.anchor = GridBagConstraints.WEST;
            gridBagConstraints11.insets = new Insets(5, 0, 5, 0);
            gridBagConstraints11.gridwidth = 2;
            gridBagConstraints11.gridx = 2;
            gridBagConstraints11.gridy = 2;
            gridBagConstraints11.weightx = 1.0;
            gridBagConstraints11.fill = GridBagConstraints.NONE;
            GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
            gridBagConstraints10.anchor = GridBagConstraints.WEST;
            gridBagConstraints10.insets = new Insets(5, 0, 5, 0);
            gridBagConstraints10.gridwidth = 2;
            gridBagConstraints10.gridx = 2;
            gridBagConstraints10.gridy = 3;
            gridBagConstraints10.weightx = 1.0;
            gridBagConstraints10.fill = GridBagConstraints.NONE;
            GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
            gridBagConstraints8.anchor = GridBagConstraints.WEST;
            gridBagConstraints8.gridx = 1;
            gridBagConstraints8.gridy = 4;
            gridBagConstraints8.insets = new Insets(0, 10, 0, 0);
            GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
            gridBagConstraints7.anchor = GridBagConstraints.WEST;
            gridBagConstraints7.gridx = 1;
            gridBagConstraints7.gridy = 2;
            gridBagConstraints7.insets = new Insets(0, 10, 0, 10);
            GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
            gridBagConstraints6.anchor = GridBagConstraints.WEST;
            gridBagConstraints6.gridx = 1;
            gridBagConstraints6.gridy = 3;
            gridBagConstraints6.insets = new Insets(0, 10, 0, 0);
            GridBagConstraints gridBagConstraints = new GridBagConstraints();
            gridBagConstraints.anchor = GridBagConstraints.SOUTHWEST;
            gridBagConstraints.gridx = -1;
            gridBagConstraints.gridy = -1;
            gridBagConstraints.insets = new Insets(5, 10, 0, 0);
            jPanelSEE = new JPanel();
            jPanelSEE.setBorder(BorderFactory.createTitledBorder("Gestion des SEE"));
            jPanelSEE.setLayout(new GridBagLayout());
            jPanelSEE.add(jLabel_semestre, gridBagConstraints);
            jPanelSEE.add(jLabel_type_enseignement, gridBagConstraints7);
            jPanelSEE.add(jLabel_nomre_heures, gridBagConstraints8);
            jPanelSEE.add(getJComboBox_matiere(), gridBagConstraints10);
            jPanelSEE.add(getJComboBox_type_enseignement(), gridBagConstraints11);
            jPanelSEE.add(getJComboBox_semestre(), gridBagConstraints1);
            jPanelSEE.add(jLabel_SEE, gridBagConstraints3);
            jPanelSEE.add(getJComboBox_SEE(), gridBagConstraints4);
            jPanelSEE.add(jLabel_matiere, gridBagConstraints6);
            jPanelSEE.add(getJTextField_nombre_heures(), gridBagConstraints14);
            jPanelSEE.add(getJPanel_description(), gridBagConstraints31);
            jPanelSEE.add(jLabel_nombre_heures_exemple, gridBagConstraints41);
            jPanelSEE.add(getJPanel1(), gridBagConstraints51);
            jPanelSEE.add(getJPanel_liste_services(), gridBagConstraints61);
        }
        return jPanelSEE;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

        if (e.getSource().equals(jComboBox_SEE)) {

            if (getJComboBox_SEE().getSelectedItem() != null) {

                //L'utilisateur veut creer une nouvelle see
                int id = ((ElementListe) jComboBox_SEE.getSelectedItem()).getId();
                refreshListeService();
               

                if (id != -1) {
                    try {
                        getJButton_service_plus().setEnabled(true);
                        getJButton_ajouter().setEnabled(false);
                        getJButton_supprimer().setEnabled(true);
                        getJButton_modifier().setEnabled(true);
                        facadeProgramme.creerSee();
                        facadeProgramme.getSee().load(id);
                        
                        getJTextField_nombre_heures().setText(String.valueOf(facadeProgramme.getSee().getNombreHeures()));
                        selectMatiere();
                        selectTypeEnseignement();
                    } catch (ExceptionMetier ex) {
                        afficherStatut(ex.toString());
                    }

                } else {
                    getJButton_service_plus().setEnabled(false);
                    getJButton_ajouter().setEnabled(true);
                    getJButton_supprimer().setEnabled(false);
                    getJButton_modifier().setEnabled(false);
                    if (getJComboBox_matiere().getItemCount() > 0) {
                        getJComboBox_matiere().setSelectedIndex(0);
                    }
                    if (getJComboBox_type_enseignement().getItemCount() > 0) {
                        getJComboBox_type_enseignement().setSelectedIndex(0);
                    }
                    getJTextField_nombre_heures().setText("");
                }
            }
        } else if (e.getSource().equals(getJComboBox_semestre())) {
            if (getJComboBox_semestre().getSelectedItem() != null) {
                refreshListeSee();
                refreshListeMatiere();
            }

        } else if (e.getSource().equals(getJButton_ajouter())) {
            try {
                recupererInfosSee();
                int nbHeures = Integer.parseInt(getJTextField_nombre_heures().getText());
                facadeProgramme.creerSee(facadeProgramme.getMatiere(), facadeProgramme.getTypeEnseignement(), nbHeures);
                facadeProgramme.ajouterSee();
                refreshListeSee();
                afficherStatut("Ajout effectué");
            } catch (ExceptionMetier ex) {
                afficherStatut(ex.toString());
            } catch (NumberFormatException ex) {
                afficherStatut(ex.toString());
            }
        } else if (e.getSource().equals(getJButton_modifier())) {
            try {
                recupererInfosSee();
                int nbHeures = Integer.parseInt(getJTextField_nombre_heures().getText());
                facadeProgramme.modifierSee(facadeProgramme.getMatiere(), facadeProgramme.getTypeEnseignement(), nbHeures);
                refreshListeSee();
                afficherStatut("Modification Effectuee");
            } catch (ExceptionMetier ex) {
                afficherStatut(ex.toString());
            } catch (NumberFormatException ex) {
                afficherStatut(ex.toString());
            }

        } else if (e.getSource().equals(getJButton_supprimer())) {
            try {
                facadeProgramme.supprimerSee();
                refreshListeSee();
                afficherStatut("Suppression Effectuee");
            } catch (ExceptionMetier ex) {
                afficherStatut(ex.toString());
            }
        } else if (e.getSource().equals(jButton_service_moins)) {
            int rep = JOptionPane.showConfirmDialog(this, mSupprimerService, "Confirmer la suppression", JOptionPane.YES_NO_OPTION);
            if (rep == 0) {
                facadeProgramme.creerService(null, null, null, null);
                try {
                    facadeProgramme.getService().load(((ElementListe) getJList_liste_services().getSelectedValue()).getId());
                    facadeProgramme.supprimerService();
                    afficherStatut("Suppression effectu�e");
                    int id = ((ElementListe) jComboBox_SEE.getSelectedItem()).getId();
                    refreshListeService();
                } catch (ExceptionMetier e1) {
                    afficherStatut(e1.toString());
                    e1.printStackTrace();
                }
            }
        }

    }

    private void afficherStatut(String message) {
        AdminGUI.jLabelStatus.setText(message);
    }

    public void valueChanged(ListSelectionEvent e) {
        if (e.getSource().equals(jList_liste_services)) {

            if (getJList_liste_services().getSelectedIndex() != -1) {
                //
                jButton_service_moins.setEnabled(true);
                jButton_service_modification.setEnabled(true);
            } else {
                jButton_service_moins.setEnabled(false);
                jButton_service_modification.setEnabled(false);
            }
        }
    }

    /**
     * This method initializes elNouvelleSalle
     *
     * @return metier.ElementListe
     */
    private ElementListe getElNouveauSee() {
        if (elNouveauSee == null) {
            elNouveauSee = new ElementListe(-1, "Nouveau SEE");
        }
        return elNouveauSee;
    }

    private void refreshListeService() {
        int id = ((ElementListe) getJComboBox_SEE().getSelectedItem()).getId();
        ArrayList<ElementListe> liste;
        try {
            liste = facadeProgramme.creerListeService(id);

            getJButton_service_modification().setEnabled(false);
            getJButton_service_moins().setEnabled(false);

            getJList_liste_services().removeAll();
            DefaultListModel modelo = new DefaultListModel();

            for (int i = 0; i < liste.size(); i++) {
                modelo.addElement(liste.get(i));
            }
            getJList_liste_services().setModel(modelo);
        } catch (ExceptionMetier e) {
            // TODO Auto-generated catch block
            afficherStatut(e.toString());
        }
    }

    private void refreshListeSemestre() {
        try {
            ArrayList<ElementListe> liste = facadeAnneeEtude.creerListeSemestre();
            ElementListe e = (ElementListe) getJComboBox_semestre().getSelectedItem();
            getJComboBox_semestre().removeAllItems();
            getJComboBox_SEE().setEnabled(false);
            getJComboBox_matiere().setEnabled(false);

            if (liste.size() > 0) {
                getJComboBox_SEE().setEnabled(true);
                getJComboBox_matiere().setEnabled(true);
            }

            for (int i = 0; i < liste.size(); i++) {

                getJComboBox_semestre().addItem(liste.get(i));
                if (e != null) {
                    if (e.getId() == liste.get(i).getId()) {
                        getJComboBox_semestre().setSelectedIndex(i);
                    }
                }
            }



            refreshListeMatiere();

            refreshListeSee();

        } catch (ExceptionMetier ex) {
            afficherStatut(ex.toString());
        }
    }

    private void refreshListeSee() {
        try {
            ArrayList<ElementListe> liste;
            getJButton_ajouter().setEnabled(false);
            getJButton_supprimer().setEnabled(false);
            getJButton_modifier().setEnabled(false);
            ElementListe e = (ElementListe) getJComboBox_SEE().getSelectedItem();
            getJComboBox_SEE().removeAllItems();
            getJComboBox_SEE().setEnabled(false);
            if (getJComboBox_semestre().getItemCount() > 0) {
                int id = ((ElementListe) getJComboBox_semestre().getSelectedItem()).getId();





                liste = facadeProgramme.creerListeSeeSemestre(id);
                getJComboBox_SEE().setEnabled(true);
                getJButton_ajouter().setEnabled(true);


                liste.add(0, getElNouveauSee());

                for (int i = 0; i < liste.size(); i++) {
                    getJComboBox_SEE().addItem(liste.get(i));
                    if (e != null) {
                        if (e.getId() == liste.get(i).getId()) {
                            getJComboBox_SEE().setSelectedIndex(i);
                        }
                    }

                }
                refreshListeService();
                //selectMatiere();
                //selectTypeEnseignement();

            }


        } catch (ExceptionMetier ex) {
            afficherStatut(ex.toString());
        }
    }

    private void refreshListeTE() {
        try {
            ArrayList<ElementListe> liste = facadeProgramme.creerListeTypeEnseignement();
            ElementListe e = (ElementListe) getJComboBox_type_enseignement().getSelectedItem();
            getJComboBox_type_enseignement().removeAllItems();
            for (int i = 0; i < liste.size(); i++) {
                getJComboBox_type_enseignement().addItem(liste.get(i));
                if (e != null) {
                    if (e.getId() == liste.get(i).getId()) {
                        getJComboBox_type_enseignement().setSelectedIndex(i);
                    }
                }
            }
        } catch (ExceptionMetier ex) {
            afficherStatut(ex.toString());
        }

    }

    private void refreshListeMatiere() {
        try {
            ArrayList<ElementListe> liste;
            ElementListe e = (ElementListe) getJComboBox_matiere().getSelectedItem();
            getJComboBox_matiere().removeAllItems();
            getJComboBox_matiere().setEnabled(false);
            if (getJComboBox_semestre().getItemCount() > 0) {
                int id = ((ElementListe) getJComboBox_semestre().getSelectedItem()).getId();


                
                liste = facadeProgramme.creerListeMatiereSemestre(id);

                if(liste.isEmpty()){
                    getJComboBox_matiere().setEnabled(false);
                    getJComboBox_SEE().setEnabled(false);
                    getJButton_ajouter().setEnabled(false);
                }
                else {
                    getJComboBox_matiere().setEnabled(true);
                }

                for (int i = 0; i < liste.size(); i++) {
                    getJComboBox_matiere().addItem(liste.get(i));
                    if (e != null) {
                        if (e.getId() == liste.get(i).getId()) {
                            getJComboBox_matiere().setSelectedIndex(i);
                        }
                    }
                }

            }
        } catch (ExceptionMetier ex) {
            afficherStatut(ex.toString());
        }

    }

    private void selectMatiere() {
        boolean trouve = false;
        int i = 0;
        if (getJComboBox_matiere().getItemCount() > i) {
            getJComboBox_matiere().setSelectedIndex(i);
        }
        if (facadeProgramme.getSee() != null) {
            if(facadeProgramme.getSee().getMatiere() != null)
                {
            int IdATrouve = facadeProgramme.getSee().getMatiere().getId();
                    

            while (i <= getJComboBox_matiere().getItemCount() - 1 && !trouve) {
                if (((ElementListe) getJComboBox_matiere().getItemAt(i)).getId() == IdATrouve) {
                    trouve = true;
                    getJComboBox_matiere().setSelectedIndex(i);
                } else {
                    i++;
                }
            }
            }
        }
    }

    private void selectTypeEnseignement() {
        boolean trouve = false;
        int i = 0;
        if (getJComboBox_type_enseignement().getItemCount() > i) {
            getJComboBox_type_enseignement().setSelectedIndex(i);
        }
        if (facadeProgramme.getSee() != null) {
            if(facadeProgramme.getSee().getTypeEnseignement() != null){

            int IdATrouve = facadeProgramme.getSee().getTypeEnseignement().getId();

            while (i <= getJComboBox_type_enseignement().getItemCount() - 1 && !trouve) {
                if (((ElementListe) getJComboBox_type_enseignement().getItemAt(i)).getId() == IdATrouve) {
                    trouve = true;
                    getJComboBox_type_enseignement().setSelectedIndex(i);
                } else {
                    i++;
                }

            }
            }
        }
    }

    private void recupererInfosSee() throws ExceptionMetier {
        int idMatiere = ((ElementListe) jComboBox_matiere.getSelectedItem()).getId();
        int idTypeEnseignement = ((ElementListe) jComboBox_type_enseignement.getSelectedItem()).getId();
        facadeProgramme.creerMatiere();
        facadeProgramme.creerTypeEnseignement();
        facadeProgramme.getTypeEnseignement().load(idTypeEnseignement);
        facadeProgramme.getMatiere().load(idMatiere);
    }
}  //  @jve:decl-index=0:visual-constraint="40,20"

